package com.bjc.concurrent;

/**
 * @program: aqs_study
 * @description:  volatile 保证可见性与有序性，但是不保证原子性
 * @author: bjc
 * @create: 2021-03-22 21:07
 */
public class VolatileAtomicTest {

    public static volatile int num = 0;

    public static void increase(){
        num++;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];

        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    increase();
                }
            });
            threads[i].start();
        }

        for (Thread thread : threads) {
            thread.join();
        }
        System.out.println(num);   //1000*10 = 10000
    }
}
